package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.context.BaseContext;
import com.ydd.context.SysBaseContext;
import com.ydd.dto.UserDataPageDTO;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddUserData;
import com.ydd.mapper.YddArticleMapper;
import com.ydd.mapper.YddUserDataMapper;
import com.ydd.result.Result;
import com.ydd.service.YddUserDataService;
import com.ydd.vo.DataListVO;
import com.ydd.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangxiaojun
 * @since 2023-05-14
 */
@Service
public class YddUserDataServiceImpl extends ServiceImpl<YddUserDataMapper, YddUserData> implements YddUserDataService {
    @Autowired
    private YddArticleMapper articleMapper;
    @Autowired
    private YddUserDataMapper dataMapper;

    /**
     * 我的收藏/点赞
     *
     * @param dto
     * @return
     */
    @Override
    public Result userDataList(UserDataPageDTO dto) {

        String userId = SysBaseContext.getCurrentId();
        //根据用户id,类型,查询文章id
        List<Integer> articleId = dataMapper.getArticleId(userId, dto.getType());
        List<YddArticle> articleList = new ArrayList<>();
        articleId.forEach(articleIds -> {
            YddArticle yddArticle = articleMapper.selectById(articleIds);
            articleList.add(yddArticle);

        });
        Page<YddUserData> page = new Page<>();
        LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
        page(page,wrapper);
        PageVO vo = PageVO.builder()
                .count((int)page.getTotal())
                .data(articleList)
                .totalPages((int)page.getPages())
                .pageSize((int)page.getSize())
                .currentPage((int)page.getCurrent())
                .build();

        DataListVO dataListVO = new DataListVO();
        dataListVO.setList(vo);


        return Result.success(dataListVO);

    }


}
